package com.goldplusgold.tradedelay.app.trade.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.widget.AsyncTask;
import com.goldplusgold.support.lib.widget.horizontallistview.SyncHorizontalScrollView;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.trade.adapter.AdapterTrigger;
import com.goldplusgold.tradedelay.app.trade.fragment.TradeAccountFragment;
import com.goldplusgold.tradedelay.app.trade.networkservice.TradeService;
import com.goldplusgold.tradedelay.app.trade.pojo.TriggeredRecord;
import com.goldplusgold.tradedelay.app.trade.pojo.TriggeredRecordList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/30.
 */

public class TriggerListActivity extends FragmentActivity {

    @BindView(R.id.iv_flip)
    ImageView ivFlip;
    @BindView(R.id.iv_slogan)
    ImageView ivSlogan;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.txt_gold_price)
    TextView txtGoldPrice;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.iv_notification)
    ImageView ivNotification;
    @BindView(R.id.iv_gold_trend)
    ImageView ivGoldTrend;
    @BindView(R.id.iv_td_more)
    ImageView ivTdMore;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.id_title_layout)
    RelativeLayout idTitleLayout;
    @BindView(R.id.tv_not_triggered)
    TextView tvNotTriggered;
    @BindView(R.id.line_not_triggered)
    View lineNotTriggered;
    @BindView(R.id.rl_not_triggered)
    RelativeLayout rlNotTriggered;
    @BindView(R.id.tv_triggered)
    TextView tvTriggered;
    @BindView(R.id.line_triggered)
    View lineTriggered;
    @BindView(R.id.rl_triggered)
    RelativeLayout rlTriggered;
    @BindView(R.id.ll_triggeredList_listHeader)
    LinearLayout llTriggeredListListHeader;
    @BindView(R.id.head_content_horsv)
    SyncHorizontalScrollView headContentHorsv;
    @BindView(R.id.id_text_view_title_position)
    TextView idTextViewTitlePosition;
    @BindView(R.id.id_bottom_list_view_position)
    PullToRefreshListView triggeredList;
    public AdapterTrigger trigerredAdapter;
    protected Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_trigger_list);
        unbinder = ButterKnife.bind(this);
        initView();
    }

    public class NotTriggeredItems extends AsyncTask<Void, Void, TriggeredRecordList> {
        String pageNo;
        String pageSize;

        public NotTriggeredItems(String pageNo, String pageSize) {
            super();
            this.pageNo = pageNo;
            this.pageSize = pageSize;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected TriggeredRecordList doInBackground(Void... params) {
            TriggeredRecordList unTriggeredRecordList = null;
            try {
                unTriggeredRecordList = TradeService.getInstance().getAllNotTriggered(pageNo, pageSize);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return unTriggeredRecordList;
        }

        @Override
        protected void onPostExecute(TriggeredRecordList unTriggeredRecordList) {
            super.onPostExecute(unTriggeredRecordList);

            if (this == null || isFinishing()) {
                return;
            }
            if (unTriggeredRecordList == null) {
                return;
            }
            setResultAction(unTriggeredRecordList);
        }

    }


    private void setResultAction(TriggeredRecordList triggeredRecordList) {
        String code = triggeredRecordList.getCode();
        if (TextUtils.isEmpty(code)) {
            return;
        }

        switch (code) {
            case "200": //请求添加数据成功, 新增一个normal条目
                List<TriggeredRecord> triggeredRecords = triggeredRecordList.getData();
                trigerredAdapter.setArrayList(new ArrayList<>(triggeredRecords));
                break;
            case "400":
            case "410":
            case "420":
            case "430":
            case "440":
            case "500":
                break;
            default:
                break;
        }

    }

    public class TriggeredItems extends AsyncTask<Void, Void, TriggeredRecordList> {
        String pageNo;
        String pageSize;

        public TriggeredItems(String pageNo, String pageSize) {
            super();
            this.pageNo = pageNo;
            this.pageSize = pageSize;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected TriggeredRecordList doInBackground(Void... params) {

            try {
                TriggeredRecordList triggeredRecordList = TradeService.getInstance().getAllTriggered(pageNo, pageSize);
                if (triggeredRecordList != null) {
                    return triggeredRecordList;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(TriggeredRecordList triggeredRecordList) {
            super.onPostExecute(triggeredRecordList);

            if (this == null || isFinishing()) {
                return;
            }
            if (triggeredRecordList == null) {
                return;
            }
            setResultAction(triggeredRecordList);
        }
    }


    public void initView() {
        ivBack.setVisibility(View.VISIBLE);
        titleTxt.setVisibility(View.VISIBLE);
        titleTxt.setText("止盈止损列表");
        trigerredAdapter = new AdapterTrigger(TriggerListActivity.this);
        triggeredList.setAdapter(trigerredAdapter);
        showUntriggeredHeaderList();
        new NotTriggeredItems("1", "10").execute(AsyncTask.TYPE_NET);
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("fromPage")) {
            String fromPage = intent.getStringExtra("fromPage");
            switch (fromPage) {
                case TradeAccountFragment.FROM_PAGE_NAME:
                    rlNotTriggered.performClick();
                    break;
                case ProfitAndLossActivity.FROM_PAGE_NAME:
                    rlTriggered.performClick();
                    break;
            }

        }

    }


    @OnClick({R.id.iv_back, R.id.rl_not_triggered, R.id.rl_triggered})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back://返回止盈止损页面
                finish();
                break;

            case R.id.rl_not_triggered: //"未触发"和其下划线变成绿色，“已触发”和其下划线变成黑色，同时展示未触发列表
                tvNotTriggered.setTextColor(Color.rgb(216, 170, 107));
                tvTriggered.setTextColor(Color.BLACK);
                lineNotTriggered.setVisibility(View.VISIBLE);
                lineTriggered.setVisibility(View.INVISIBLE);

                //清除头部之前内容，展示未触发列表
                llTriggeredListListHeader.removeAllViews();
                showUntriggeredHeaderList();

                //展示未触发列表
                new NotTriggeredItems("1", "10").execute(AsyncTask.TYPE_NET);
                break;

            case R.id.rl_triggered: //"已触发"和其下划线变成绿色，“未触发”和其下划线变成黑色，同时展示已触发列表
                tvTriggered.setTextColor(Color.rgb(216, 170, 107));
                tvNotTriggered.setTextColor(Color.BLACK);
                lineTriggered.setVisibility(View.VISIBLE);
                lineNotTriggered.setVisibility(View.INVISIBLE);
                //展示已触发列表
                llTriggeredListListHeader.removeAllViews();
                showTriggeredHeaderList();
                new TriggeredItems("1", "10").execute(AsyncTask.TYPE_NET);
                break;
        }
    }


    public void showUntriggeredHeaderList() {
        List<String> unTriggeredTitles = Arrays.asList(
                "多/空",
                "类型",
                "价格",
                "滑点价",
                "手数",
                "创建时间",
                "状态");
        for (String str : unTriggeredTitles) {
            View childView = LayoutInflater.from(this).inflate(R.layout.item_horizontal_position, llTriggeredListListHeader, false);
            TextView mTextViewHorizontal = (TextView) childView.findViewById(R.id.id_text_view_horizontal);
            mTextViewHorizontal.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
            mTextViewHorizontal.setText(str);
            mTextViewHorizontal.setTextColor(Color.rgb(102, 102, 102));
            llTriggeredListListHeader.addView(childView);
        }
        trigerredAdapter.addHead(headContentHorsv);
        headContentHorsv.setOnScrollListenter(trigerredAdapter);
    }

    public void showTriggeredHeaderList() {
        List<String> triggeredTitles = Arrays.asList(
                "多/空",
                "类型",
                "价格",
                "手数",
                "委托价",
                "触发时间",
                "状态");
        for (String str : triggeredTitles) {
            View childView = LayoutInflater.from(this).inflate(R.layout.item_horizontal_position, llTriggeredListListHeader, false);
            TextView mTextViewHorizontal = (TextView) childView.findViewById(R.id.id_text_view_horizontal);
            mTextViewHorizontal.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
            mTextViewHorizontal.setText(str);
            mTextViewHorizontal.setTextColor(Color.rgb(102, 102, 102));
            llTriggeredListListHeader.addView(childView);
        }
        trigerredAdapter.addHead(headContentHorsv);
        headContentHorsv.setOnScrollListenter(trigerredAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}




